import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../views/Welcome'
import User from '../views/user/User'
import Rights from '../views/power/rights'
import Route from '../views/power/route'
import Cate from '../views/goods/Cate'


Vue.use(VueRouter)


const router = new VueRouter({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home',
            component: Home,
            redirect: '/home/welcome',
            children: [
                { path: 'welcome', component: Welcome },
                { path: '/users', component: User },
                { path: '/rights', component: Rights },
                { path: '/roles', component: Route },
                { path: '/categories', component: Cate },
            ]
        },
    ]
})
router.beforeEach((to, form, next) => {
    if (to.path === '/login') return next()
    const tokenStr = localStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})
export default router